/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import dao.HistoricoDao;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import model.Historico;
import org.hibernate.CacheMode;
import org.hibernate.Criteria;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.type.Type;
import util.HibernateUtil;

/**
 *
 * @author rafael
 */
public class HistoricoDaoImpl implements HistoricoDao {

    @Override
    public void inserir(Historico historico) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(historico);
            session.beginTransaction().commit();
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            System.out.println(e.getLocalizedMessage());
            GrowMessages.errorMessage(e.getLocalizedMessage(), "Erro");
        } finally {
            session.close();
        }
    }

    @Override
    public void eliminar(Historico historico) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {

            session.beginTransaction();
            session.delete(historico);
            session.beginTransaction().commit();

        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage(e.getLocalizedMessage(), "Erro");
            System.out.println(e.getLocalizedMessage());
        } finally {
            session.close();
        }
    }

    @Override
    public List<Historico> selectByUsuarioCompras(Integer pkId) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria criteria = session.createCriteria(Historico.class);
            Criterion compra = Restrictions.eq("usuariosByFkUsuarioComprador.pkId", pkId);
            criteria.add(compra);
            return criteria.list();

        } catch (HibernateException hibernateException) {
            GrowMessages.errorMessage("Erro", hibernateException.getLocalizedMessage());
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public List<Historico> selectByUsuarioVendas(Integer pkId) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria criteria = session.createCriteria(Historico.class);
            Criterion compra = Restrictions.eq("usuariosByFkUsuarioVendedor.pkId", pkId);
            criteria.add(compra);
            return criteria.list();

        } catch (HibernateException hibernateException) {
            GrowMessages.errorMessage("Erro", hibernateException.getLocalizedMessage());
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }
}
